package net.hubt.itc.ui;

/***********************************************************************
 * 																	   *
 *Programmer: Tiep, Nguyen											   *
 *																	   *
 *Last Modify: Nov, 23th, 2012										   *
 *Add ClassLookActivity												   *
 *Version: 1.0														   *
 *Read xml file, filter and add to 3 spinners, add mLop to Preferences *
 ***********************************************************************/

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import net.hubt.itc.data.ClassLookSpin;
import net.hubt.itc.data.ClassLookXMLParser;
import net.hubt.itc.util.XMLDocument;


import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

import android.content.Intent;
import android.content.res.AssetManager;


public class ClassLookActivity extends Activity {


	private Spinner spinKhoaHoc, spinNganh, spinLop;
	private Button btnSubmit;
	private ClassLookSpin m_ClassLook; 
	private String mKhoahoc;
	private String mNganh;
	private String mLop;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.classlook_layout);
		m_ClassLook = new ClassLookSpin();
		

		// Load XML for parsing.
        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open("thoikhoabieu.xml");
        } catch (IOException e) {
            //Log.e("tag", e.getMessage());
        }

        String xmlData = readTextFile(inputStream);
        XMLDocument xmlDoc = new XMLDocument();
		//Document document = xmlDoc.getDocument(xmlData);

		ClassLookXMLParser clp = new ClassLookXMLParser();
		clp.setRoot(xmlDoc.getRootNode(xmlData));

		m_ClassLook.setarr_All(clp.parseXmlData());
		
			
		addItemsOnSpinnerKhoaHoc();
		addListenerOnButton();
		addListenerOnSpinnerItemSelection();
		
	}

	//add items into spinner dynamically
	
	public void addItemsOnSpinnerKhoaHoc(){
		m_ClassLook.filterKhoaHoc();
		spinKhoaHoc = (Spinner) findViewById(R.id.spinKhoaHoc);

		
		List<String> list =new ArrayList<String>();
	
		for (int i=0;i<m_ClassLook.getsizeKhoaHoc();i++)
			list.add(m_ClassLook.getKhoaHoc(i));
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(R.drawable.blue_theme_dropdowstextitems);
		
		dataAdapter.notifyDataSetChanged();
		spinKhoaHoc.setAdapter(dataAdapter);
	}
	public void addItemsOnSpinnerNganh(String khoahoc) {

		
		m_ClassLook.filterNganh(khoahoc);
			
		spinNganh = (Spinner) findViewById(R.id.spinNganh);

		
		List<String> list =new ArrayList<String>();
	
		for (int i=0;i<m_ClassLook.getsizeNganh();i++)
			list.add(m_ClassLook.getNganh(i));
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(R.drawable.blue_theme_dropdowstextitems);
		
		dataAdapter.notifyDataSetChanged();
		spinNganh.setAdapter(dataAdapter);
	}
	public void addItemsOnSpinnerLop(String khoahoc, String nganh) {
		m_ClassLook.filterLop(khoahoc, nganh);
		spinLop = (Spinner) findViewById(R.id.spinLop);

		List<String> list =new ArrayList<String>();
	
		for (int i=0;i<m_ClassLook.getsizeLop();i++)
			list.add(m_ClassLook.getLop(i));
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(R.drawable.blue_theme_dropdowstextitems);
		
		dataAdapter.notifyDataSetChanged();	
		spinLop.setAdapter(dataAdapter);
	}
	public void addListenerOnSpinnerItemSelection(){
		
		spinKhoaHoc = (Spinner) findViewById(R.id.spinKhoaHoc);
		spinKhoaHoc.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos,
					long id) {
				
				mKhoahoc = parent.getSelectedItem().toString();
				addItemsOnSpinnerNganh(mKhoahoc);
			}
		
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			};
		});
		
		
		spinNganh = (Spinner) findViewById(R.id.spinNganh);
		spinNganh.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos,
					long id) {
				mNganh = String.valueOf(spinNganh.getSelectedItem());
				addItemsOnSpinnerLop(mKhoahoc,mNganh);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			};
		});
		
	}
	

	//get the selected dropdown list value
	public void addListenerOnButton() {

		spinKhoaHoc = (Spinner) findViewById(R.id.spinKhoaHoc);
		spinNganh = (Spinner) findViewById(R.id.spinNganh);
		
		btnSubmit = (Button) findViewById(R.id.btnSubmit);

		btnSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				

				
				mLop = 	String.valueOf(spinLop.getSelectedItem());
				HUBTApplication applicationContext= (HUBTApplication)getApplicationContext();
				applicationContext.setClassName(mLop);
				
				Intent intent = new Intent(ClassLookActivity.this, TimeTableActivity.class);
		        startActivity(intent);
				
/*				Toast.makeText(ClassLookActivity.this,
						"OnClickListener : " + 
						"\nSpinner 1 : " + String.valueOf(spinKhoaHoc.getSelectedItem()) +
						"\nSpinner 2 : " + String.valueOf(spinNganh.getSelectedItem())   +
						"\nSpinner 3 : " + String.valueOf(spinLop.getSelectedItem()),
						Toast.LENGTH_SHORT).show();
*/			}

		});

	}
	private String readTextFile(InputStream inputStream) {
	    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

	    byte buf[] = new byte[1024];
	    int len;
	    try {
	        while ((len = inputStream.read(buf)) != -1) {
	            outputStream.write(buf, 0, len);
	        }
	        outputStream.close();
	        inputStream.close();
	    } catch (IOException e) {

	    }
	    return outputStream.toString();
	}

}
